package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtGoods;
import com.csht.model.CshtShelves;
import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtGoodsService;
import com.csht.service.CshtShelvesService;
import com.csht.vo.my.CshtGoodsVo_3;

/**
 * 出售中的商品controller
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class SellGoodsController {
	
	@Autowired
	private CshtGoodsService cshtGoodsService;
	
	@Autowired
	private CshtShelvesService cshtShelvesService;

	/**
	 * 个人中心－出售中的商品
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sell")
	public String sell(ModelMap model, HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Map<String, Integer> res = new HashMap<String, Integer>();
		res.put("userId", cshtUser.getUserId());
		res.put("messagePageSize", SystemContext.getMessagePageSize());
		res.put("offset", SystemContext.getOffset());
		PagerModel<CshtGoodsVo_3> pagerModel = cshtGoodsService.getSellGoodsByUserId(res);
		model.addAttribute("pagerModel", pagerModel);
		return "/toMy/sellGoods";
	}
	
	/**
	 * 出售中的商品下架
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/sellGoodsShelves")
	public Map<String, Object> sellGoodsShelves(@RequestParam(name = "goodsId") Integer goodsId) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		CshtGoods cshtGoods = cshtGoodsService.selectGoodsById(goodsId); //根据商品id查找到商品
		cshtGoodsService.updateGoodsStateById(goodsId); //根据商品id更新商品为状态为"已下架"
		//给cshtShelves赋值
		CshtShelves cshtShelves = new CshtShelves();
		cshtShelves.setBrandId(cshtGoods.getBrandId());
		cshtShelves.setGoodsId(cshtGoods.getGoodsId());
		cshtShelves.setOneMenuId(cshtGoods.getOneMenuId());
		cshtShelves.setTwoMenuId(cshtGoods.getTwoMenuId());
		cshtShelves.setThreeMenuId(cshtGoods.getThreeMenuId());
		cshtShelves.setUserId(cshtGoods.getUserId());
		cshtShelves.setShelvesBrowseTime(cshtGoods.getGoodsBrowseTime());
		cshtShelves.setShelvesContent(cshtGoods.getGoodsContent());
		cshtShelves.setShelvesImgpath(cshtGoods.getGoodsImgpath());
		cshtShelves.setShelvesPayMoney(cshtGoods.getGoodsPayMoney());
		cshtShelves.setShelvesPrice(cshtGoods.getGoodsPrice());
		cshtShelves.setShelvesTitle(cshtGoods.getGoodsTitle());
		cshtShelves.setShelvesTradeSchool(cshtGoods.getGoodsTradeSchool());
		cshtShelves.setShelvesTradeWay(cshtGoods.getGoodsTradeWay());
		cshtShelves.setShelvesShelvesType("自行下架");
		cshtShelvesService.insertShelvesGoods(cshtShelves); //插入下架商品记录
		res.put("result", true);
		return res;
	}
	
}
